package com.usc.mock.db.buffer;

import com.usc.mock.db.bean.UserInfo;
import com.usc.mock.db.mapper.UserInfoMapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class BufferServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> {

    List dbList = new ArrayList();

    public BufferServiceImpl() {
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        executorService.submit(() -> {
            while (true) {

                Type type = this.getClass().getGenericSuperclass();
                ParameterizedType pt = (ParameterizedType) type;
                Type[] actualTypeArguments = pt.getActualTypeArguments();
                Type actualTypeArgument = actualTypeArguments[1];
                BufferQueue bufferQueue = BufferUtil.getQueue((Class<T>) actualTypeArgument);
                List outputList = bufferQueue.getOutputList();
                while (outputList.isEmpty()) {
                    System.err.println("当前" + type + "队列个数：" + bufferQueue.getInputListSize());
                    if (bufferQueue.getInputListSize() > 1000) {
                        System.err.println("交换！！！！！！！！!!!!!!!!!!!!!!!!!!!!!!!!");
                        bufferQueue.swap();
                        outputList = bufferQueue.getOutputList();
                    } else {
                        Thread.sleep(1000);
                    }
                }
                System.out.println("保存：行数" + outputList.size());

                dbList.addAll(outputList);
                System.out.println("总：行数" + dbList.size());
                //saveBatch(outputList,1000);
                outputList.clear();
            }
        });

    }

    public void saveBatchWithBuffer(T t) {
//        Type[] genericInterfaces = getClass().getGenericInterfaces();
//        Type type = genericInterfaces[0];
        BufferQueue queue = BufferUtil.getQueue(t.getClass());
        queue.push(t);

    }

    public static void main(String[] args) {
//        List<String> list = new ArrayList<String>() ;
//        BufferServiceImpl<UserInfoMapper, UserInfo> bufferService = new BufferServiceImpl<UserInfoMapper, UserInfo>(){};
//        Type clazz = bufferService.getClass().getGenericSuperclass();
//        ParameterizedType pt = (ParameterizedType)clazz;
//        String s = pt.getActualTypeArguments()[1].toString();
//        System.out.println(s);

        BufferServiceImpl<UserInfoMapper, UserInfo> bufferService = new BufferServiceImpl<UserInfoMapper, UserInfo>() {
        };
        List dbList = new LinkedList();
        for (long i = 0; i < 100000000; i++) {
            UserInfo userInfo = new UserInfo();
            userInfo.setId(i);
            userInfo.setName("name_" + i);
            //bufferService.saveBatchWithBuffer(userInfo);
            dbList.add(userInfo);
            if (i % 1000000 == 0) {
                System.out.println("总：行数" + dbList.size());
            }
        }


    }

    @Data
    @AllArgsConstructor
    private static class Customer {
        Integer id;
        String name;
    }


}
